package com.jianwei.web.action;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.jianwei.web.pojo.Users;
import com.jianwei.web.service.UserService;
import com.jianwei.web.util.StrutsUtils;
import com.opensymphony.xwork2.ActionSupport;
@Controller("userAction")
public class UserAction extends ActionSupport {
	/**
	 *
	 */
	private static final long serialVersionUID = -3552022734232888725L;
	private Users user;
	@Autowired
	private UserService userService;

	/**
	 * 向前端输出一段json数据
	 */
	public void readJson(){
		String Json = userService.readJson();
		StrutsUtils.readHtml(Json);
	}
	
	/**
	 * 新添加用户
	 * @return
	 */
	public String addUser(){
		if(StringUtils.isEmpty(user.getUser_name())||StringUtils.isEmpty(user.getUser_pwd())){
			return ERROR;
		}else{
			userService.addUser(user);
		}
		return SUCCESS;
	}
	
	/**
	 * 查询并显示所有用户
	 */
	public void findAllUser(){
		String userJson = userService.findAllUsers();
		StrutsUtils.readJson(userJson);
	}
	
	/**
	 * 登出
	 */
	public void userLogoff(){
		userService.userLogoff();
	}
	
	/**
	 * 关闭浏览器时销毁session
	 */
	public void closeIE(){
		System.out.println("CLose IE!");
		StrutsUtils.getSession().invalidate();
	}
	
	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}
}
